package cn.htsyb.synergy.security.entity;

import cn.htsyb.synergy.orm.jpa.model.entity.AbsStructureEntity;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.v3.oas.annotations.media.Schema;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.Formula;

/**
 * 访问资源 实体类
 * @author lakeside hubin_yuan.410@hotmail.com
 * @version V1.0.0
 * @date 2022/5/23
 * @since 1.0
 */
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonIgnoreProperties(ignoreUnknown = true)
@Setter
@Getter
@NoArgsConstructor
@Entity
@DynamicInsert
@Table(name = "security_permission")
@org.hibernate.annotations.Table(appliesTo = "security_permission", comment = "资源权限")
@Schema(description = "资源权限")
public class SecurityPermission extends AbsStructureEntity {
    private static final long serialVersionUID = 5876857055953597120L;

    /**
     * 权限编码
     */
    @Column(name = "auth_code", length = 50, columnDefinition = " VARCHAR(50) COMMENT '权限编码' ")
    @Schema(name = "authCode", description = "权限编码")
    protected String authCode;


    /**
     * 访问路径
     */
    @Column(name = "visit_url", length = 250, columnDefinition = " VARCHAR(250) COMMENT '访问路径' ")
    @Schema(name = "visitUrl", description = "访问路径")
    protected String visitUrl;


    /**
     * 归属系统
     */
    @NotNull
    @Column(name = "vested_system", length = 36, columnDefinition = " VARCHAR(36) COMMENT '归属系统' ")
    @Schema(name = "vestedSystem", description = "归属系统",  required = true)
    protected String vestedSystem;
    /**
     * 权限模型回显
     *//*
    @Formula("(select sd.name_ from system_dictionary sd where sd.value_= model_ and sd.parent_id='" + FinalDictConst.AUTH_MODE + "')")
    protected String modelName;*/
    /**
     * 上级回显
     */
    @Formula("(SELECT sp.name_ FROM security_permission sp WHERE sp.id_= parent_id)")
    private String parentIdName;

    @Override
    public boolean equals(Object object) {
        if (!(object instanceof SecurityPermission)) {
            return false;
        }
        SecurityPermission permission = (SecurityPermission) object;
        return permission.authCode.equals(authCode);
    }

    @Override
    public int hashCode() {
        return authCode.hashCode();
    }
}